C++ Primier chapter six:function


in C++ function can be overload, the same name may be from different function.
function call: initialize the function`s parameter, transfer control to the function.
local static object is initialized before the first execution ,not destroyed untill the function ends.
Passing argument by value:the value of the initiaizer is copied,changes made to the variable have no effect on the initialzizer.
Pointer Parameter :copy the value of another pointer,but can change the value of the object by assignning through the pointer.
Passing Argument byreference:changes made to the reference are made to the object to which referencr refers.
Using Reference Parameters to Return Additional Information:
return multiple results effectively.
const Parameter and Arguments:top-level const that applt to the object itself.
Two Way to array:

  1. print(const int beg, const int end)
  2. print(const int ia[], size_t size)
1
2
int *matrix[10]; // array of ten pointers
int (*matrix)[10]; // pointer to an array of ten ints

initializer_list: all const value ,no way to change the value of an element.
begin():a pointer to the first element.

end():an off-the-end pointer one past the last element.

**Swap In Reference

1
2
3
4
5
6
7
8
9
10
11
void swap(int &v1, int &v2)
{
// if the values are already the same, no need to swap, just return
if (v1 == v2)
return;
// if we're here, there's work to do
int tmp = v2;
v2 = v1;
v1 = tmp;
// no explicit return necessary
}

return reference must not refer to an local object,as well as pointer.
Calls to functions that return references are lvalues; other return types
yield rvalues

Overlaod function

Functions that have the same name but different parameter lists and that appear in the same scope are overloaded
Overload function must differ in hte number or the types of their aprameters.
Top-level const has no effect on the objects that can be passed to the function.
function taking const and nonconst reference or pointers have different parameters.
In C++, name lookup happens before type checking.
the compiler first looks for a declaration of that name.
Once a name is found, the compiler
ignores uses of that name in any outer scope’

Default Argument Declarations
any subsequent declaration can add a
default only for a parameter that has not previously had a default specified
Inline Function:remove the run-time of calling a function.
put the key word inline before the return type.
A constexpr function is not required to return a constant expression.


assert(expr);
evaluate expr and if the expression is false, then assert writes a message and terminates the program.
NDEBUG
if NDEBUG is defined assert does nothing.
if NDEBUG is not defined, the code between #ifndef and the #endif is executed.
Function match

  1. First step:identifies the set of overloaded functions considered for the call. a function with the same name as the called function and for
    which a declaration is visible at the point of the call
  2. Second step:select viable function. have the same number of parameters as there are arguments in the call. and the type of each arguement must match -or be convertible to.
  3. third step: select the viable function for which the corresponding parameter best match the arguement.

Casts should not be needed to call an overloaded function. The need for a
cast suggests that the parameter sets are designed poorly.

1
2
3
4
5
6
7
8
9
10
11
1. An exact match. An exact match happens when:
• The argument and parameter types are identical.
• The argument is converted from an array or function type to the corresponding
pointer type. (§ 6.7 (p. 247) covers function pointers.)
• A top-level const is added to or discarded from the argument.
2. Match through a const conversion (§ 4.11.2, p. 162).
3. Match through a promotion (§ 4.11.1, p. 160).
4. Match through an arithmetic (§ 4.11.1, p. 159) or pointer conversion (§ 4.11.2,
p. 161).
5. Match through a class-type conversion. (§ 14.9 (p. 579) covers these
conversions.)

Pointer to Function

When we use the name of a function as a value, the function is automatically converted to a pointer
pointers to different function type can`t conversion,but can be assigned nullptr or a zero-valued integer constant expression.
When we pass a function as an argument, we can do so directly. It will be
automatically converted to a pointer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int func(int a, int b);
using pFunc1 = decltype(func) *;
typedef decltype(func) *pFunc2;
using pFunc3 = int (*)(int a, int b);
using pFunc4 = int(int a, int b);
typedef int(*pFunc5)(int a, int b);
using pFunc6 = decltype(func);
std::vector<pFunc1> vec1;
std::vector<pFunc2> vec2;
std::vector<pFunc3> vec3;
std::vector<pFunc4*> vec4;
std::vector<pFunc5> vec5;
std::vector<pFunc6*> vec6;
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
std::vector<decltype(func) *> vec{add, subtract, multiply, divide};
for (auto f : vec)
std::cout << f(2, 2) << std::endl;